package com.spsoft.vatoutinv.service.negativestream.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.exception.BusinessException;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.SecurityUtils;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.system.entity.NvatNegativestream;
import com.spsoft.vatoutinv.enums.NegeClEnum;
import com.spsoft.vatoutinv.mapper.NvatNegativestreamForNegaMapper;
import com.spsoft.vatoutinv.mapper.NvatTrandtadvaltaxsepForNegaMapper;
import com.spsoft.vatoutinv.model.dto.negativestream.*;
import com.spsoft.vatoutinv.model.vo.negativestream.NegativestreamCombineLockVo;
import com.spsoft.vatoutinv.model.vo.negativestream.NegativestreamDealVo;
import com.spsoft.vatoutinv.model.vo.negativestream.NegativestreamForViewVo;
import com.spsoft.vatoutinv.model.vo.negativestream.NegativestreamQueryVo;
import com.spsoft.vatoutinv.service.negativestream.NvatNegativestreamForVatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 负数流水管理表 服务实现类
 * </p>
 *
 * @author dingchengyun
 * @since 2024-02-26
 */
@Service
@RequiredArgsConstructor
public class NvatNegativestreamForVatServiceImpl extends ServiceImpl<NvatNegativestreamForNegaMapper, NvatNegativestream> implements NvatNegativestreamForVatService {

    private final NvatNegativestreamForNegaMapper nvatNegativestreamForNegaMapper;

    private final NvatTrandtadvaltaxsepForNegaMapper nvatTrandtadvaltaxsepForNegaMapper;

    @Override
    public Result<IPage<NegativestreamQueryVo>> queryForPage(NegativestreamQueryPageDto params) {
        // 获取分页数据
        Page<NegativestreamQueryVo> page = new Page<>(params.getPageNo(), params.getPageSize());
        String jyjg = params.getJyjg();
        //将交易机构按照,分割成list
        if (StringUtil.isNotEmpty(jyjg)) {
            List<String> jyjgList = Arrays.asList(jyjg.split(","));
            params.setJyjgList(jyjgList);
        } else {
            params.setJyjgList(Arrays.asList(SecurityUtils.getDeptCode()));
        }
        IPage<NegativestreamQueryVo> negativestreamQueryVoIPage = nvatNegativestreamForNegaMapper.queryForPage(page, params);
        return Result.OK(negativestreamQueryVoIPage);
    }

    @Override
    public boolean updateNeFocusByid(String id) {
        String sfgz = nvatNegativestreamForNegaMapper.querySfgzByid(Long.valueOf(id));
        if (StringUtil.isNotEmpty(sfgz) && "0".equals(sfgz)) {
            nvatNegativestreamForNegaMapper.updateSfgzById(Long.valueOf(id));
        } else {
            return false;
        }
        return true;
    }

    @Override
    public boolean releaseByid(NegativesCommonDealDto params) {
        //释放负数流水管理表数据
        List<Long> wkpRdidList = new ArrayList<>();
        wkpRdidList.add(params.getRdid());
        boolean b = nvatNegativestreamForNegaMapper.rollbackByRdids(wkpRdidList);
        //如果处理类型为犹豫期退得，则还原正负流水流水
        if (NegeClEnum.CLLX_01.getKey().equals(params.getCllx()) && StringUtil.isNotBlank(params.getYcl_rdid())) {
            //还原正负流水流水
            nvatTrandtadvaltaxsepForNegaMapper.updateLsztToNormalByRdids(Arrays.asList(params.getRdid(), Long.valueOf(params.getYcl_rdid())));
        }
        return b;
    }

    @Override
    public Result<NegativestreamDealVo> queryByRdid(String rdid) {
        NegativestreamDealVo negativestreamDealVo = nvatTrandtadvaltaxsepForNegaMapper.queryNegativestreamDealVoByRdid(Long.valueOf(rdid));
        return Result.OK(negativestreamDealVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dealByRdid(NegativesDealDto params) {
        params.setCzr(SecurityUtils.getUsername());
        //处理负数流水管理表数据
        nvatNegativestreamForNegaMapper.updateNegaByRdids(params);
        //锁定流水
        nvatTrandtadvaltaxsepForNegaMapper.updateLsztByRdids(Arrays.asList(params.getRdid()));
        return true;
    }

    @Override
    public Result<NegativestreamForViewVo> queryForView(NegativeForViewDto params) {
        NegativestreamForViewVo negativestreamForViewVo = nvatNegativestreamForNegaMapper.queryNegaByid(Long.valueOf(params.getId()));
        if(negativestreamForViewVo==null){
            throw new BusinessException(ResponseEnum.ERROR.getCode(), "负数流水信息不存在");
        }
        NegativestreamDealVo fuVo = nvatTrandtadvaltaxsepForNegaMapper.queryNegativestreamDealVoByRdid(Long.valueOf(params.getRdid()));
        if(StringUtil.isNotEmpty(params.getYcl_rdid())){
            NegativestreamDealVo zhengVo = nvatTrandtadvaltaxsepForNegaMapper.queryNegativestreamDealVoByRdid(Long.valueOf(params.getYcl_rdid()));
            negativestreamForViewVo.setZhengVo(zhengVo);
        }
        negativestreamForViewVo.setFuVo(fuVo);
        return Result.OK(negativestreamForViewVo);
    }

    @Override
    public Result<IPage<NegativestreamCombineLockVo>> queryCombineLock(NegativestreamCombineLockQueryPageDto params) {
        Page<NegativestreamCombineLockVo> page = new Page<>(params.getPageNo(), params.getPageSize());
        params.setJyjg(SecurityUtils.getDeptCode());//机构需要从选择的数据中获取，不能操作下级机构的数据
        IPage<NegativestreamCombineLockVo> negativestreamCombineLockVoIPage = nvatTrandtadvaltaxsepForNegaMapper.queryCombineLock(page, params);
        return Result.OK(negativestreamCombineLockVoIPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result dealCombineLockByRdid(NegativesDealDto params) {
        //分别查询出两条对应的流水，并针对流水进行对比，按照需求要求的一致性进行对比
        NegativestreamDealVo fuVo = nvatTrandtadvaltaxsepForNegaMapper.queryNegativestreamDealVoByRdid(params.getRdid());
        NegativestreamDealVo zhengVo = nvatTrandtadvaltaxsepForNegaMapper.queryNegativestreamDealVoByRdid(Long.valueOf(params.getYcl_rdid()));
        if (fuVo != null && zhengVo != null) {
            BigDecimal fsjyje = new BigDecimal(fuVo.getJyrmbje());
            BigDecimal zjyje = new BigDecimal(zhengVo.getJyrmbje());
            boolean jyjeFlag = false;
            if (fsjyje.add(zjyje).compareTo(BigDecimal.ZERO) == 0) {
                jyjeFlag = true;
            }
            // 判断交易金额是否一致且交易税率、投保人、险种简称是否一致
            if (jyjeFlag && fuVo.getSlv().equals(zhengVo.getSlv()) && fuVo.getKfhm().equals(zhengVo.getKfhm()) && fuVo.getXzjc().equals(zhengVo.getXzjc())) {
                params.setCzr(SecurityUtils.getUsername());
                params.setCzsm(NegeClEnum.CLLX_07.getValue());
                params.setCllx(NegeClEnum.CLLX_07.getKey());
                //更新负数留观管理表和流水表
                nvatNegativestreamForNegaMapper.updateNegaByRdids(params);
                nvatTrandtadvaltaxsepForNegaMapper.updateLsztByRdids(Arrays.asList(params.getRdid(), Long.valueOf(params.getYcl_rdid())));
            } else {
                if (!jyjeFlag) {
                    return Result.ERROR(ResponseEnum.NEGA_COMBINE_ERR);
                }
                return Result.ERROR(ResponseEnum.NEGA_COMBINE_ERR_01);
            }
        }else{
            return Result.ERROR(ResponseEnum.NEGA_COMBINE_ERR_02);
        }
        return Result.OK();

    }


}
